// pages/Settlement/Offline/Offline.js
const controllers = require('../../controllers/controllerIndex.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    index:0,
    selectItem:{
      mobile:"",
      nick:"",
      chooseProduct:[]
    },
    remarkContent:"",
    showAdd:true,
    remark:"",
    input:null,
    chooseProduct:[],
    input:null
  },

  bindKeyInput(res) {
    console.log(res)
    this.setData({
      input: res.detail.value
    })


  },
  bindPickerChange(res){
    let name =this.data.peopleList[res.detail.value].nick
    let shopAccountId=this.data.peopleList[res.detail.value].id
    this.setData({
      choosePeople:name,
      shopAccountId:shopAccountId
    })
  },
  cancel() {
    wx.navigateBack({

    })

  },
  clickBtn(res) {

    let index = this.data.chooseProduct.findIndex(item => {
      return item.id == res.detail;
    });
    let preReplace = this.data.chooseProduct;
    if (index != -1) {
      preReplace.splice(index, 1);
    }
    this.setData({
      chooseProduct: preReplace
    })

  },
  optionTap(res) {
    var that = this;
    controllers.selectByMobile({
      sessionId: wx.getStorageSync('token'),
      shopId: wx.getStorageSync('shopId'),
      mobile: this.data.input
    }).then(res => {
      that.setData({
        userList: res.data
      })

    })
  },

  selectUser(res){
  
  
   this.setData({
     selectId: res.currentTarget.dataset.item.id,
     selectItem: res.currentTarget.dataset.item,
     show:false,
     showAdd:true
   })
  },
  remarkInput(res){
    this.setData({
      remark:res.detail.dataset
    })
  },
  addproduct() {
      if(this.data.haveGoods){
        wx.showToast({
          title: '该店铺无商品可添加',
        })
      }
    let nowListSting = JSON.stringify(this.data.chooseProduct);
    wx.navigateTo({
      url: '/pages/settlement/AddGoods/AddGoods?nowList=' + nowListSting,
    })
  },

  onAddAccount() {
    let mobile = this.data.input||"";
    let url = '/pages/userStatistics/UserEdit/UserEdit?callback=onAccountResult&mobile='+mobile;
    wx.navigateTo({url:url})
  },
  onAccountResult(res) {
    let that = this;
    let params={
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId'),
      accountRelationId:res.id
    }
    controllers.accountDetail(params).then((res)=>{//转换对象
      let data = res.data;
      let item = {
        id:data.id,
        mobile:data.accountMobile,
        nick:data.accountNick
      };
      that.setData({
        selectId: item.id,
        selectItem: item,
        show:false
      });
    });

  },

  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   console.log(options); 
   let value=JSON.parse(options.detail||null);
   console.log(value);
   if(value){
     value.goods.count=1;
     let goods=[];
     goods.push(value.goods)
     this.setData({
       'selectItem.mobile':value.accountMobile,
       'selectItem.nick':value.accountNick,
       'selectItem.id':value.accountRelationId,
       chooseProduct:goods,
       showAdd:false,
       remarkContent:value.desc,
       remark:value.desc
     })
   }
   debugger;
  controllers.staffList({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId')
    }).then(res =>{
      this.setData({
        peopleList: res.data
      })
     
    })
    controllers.haveGoods({
      shopId: wx.getStorageSync('shopId'),
      
    }).then(res =>{
      let have=res.data.id==null
      this.setData({
        haveGoods: have
      })
    })

   
  },
  confirm(){

 let that =this
 let goodsIdNum=""
 
 if( !this.data.chooseProduct){
  wx.showToast({
    title: '请添加商品',
  })
  return
}
// if( !this.data.selectItem){
//   wx.showToast({
//     title: '请选择用户',
//   })
//   return
// }
 
    this.data.chooseProduct.forEach((item,index) =>{
      let end=(index==that.data.chooseProduct.length-1)?"":","
      goodsIdNum=goodsIdNum+item.id+"-"+item.count+end
      
    })
   let shopAccountId= that.data.shopAccountId
 let json ={
 
  goodsIdNum:goodsIdNum,
  sessionId: wx.getStorageSync('token'),
  shopId:wx.getStorageSync('shopId'),
 
 
 }

 if( this.data.selectItem){
  json.accountRelationId=that.data.selectItem.id
 }

 if(shopAccountId){
  json.shopAccountId=shopAccountId
 }
if( that.data.remark){ 
      json.desc=that.data.remark}

      debugger;
 controllers.saveOrder(json).then(res =>{
  if(res.code==1){
    const that = this;
    const pages = getCurrentPages();
    const beforePage = pages[pages.length - 2];  

    // wx.navigateBack({
    //   delta: 1,
    //   success: ()=>{
        
      

    //     wx.showToast({
    //       title: '开单成功',
    //       duration: 1000,
    //       success: function () {
    //       setTimeout(function () {
    //         wx.navigateTo({
    //           url: 'pages/order/OrderRecord/OrderRecord',
    //         })
    //       }, 1000);
    //      }
    //    })
    //   }
    // })
    wx.showToast({
      title: '开单成功',
      duration: 1000,
      success: function () {
      setTimeout(function () {
        wx.navigateTo({
          url: 'pages/order/OrderRecord/OrderRecord',
        })
      }, 1000);
     }
   })
   setTimeout(function(){
      wx.navigateTo({
        url: '/pages/settlement/Offline/OfflineSuccess/OfflineSucces',
      })
   },1000)

  }
 
})


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})